package cn.bdqn.controller;

import cn.bdqn.pojo.VehicleComplaints;
import cn.bdqn.service.BrandService;
import cn.bdqn.service.VehicleComplaintsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/veh")
public class VehicleComplaintsController {
    @Resource
    VehicleComplaintsService vehicleComplaintsService;
    @Resource
    BrandService brandService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "0") Integer brandId, Model model){
        List<VehicleComplaints> lists = vehicleComplaintsService.selectAll((long)brandId);
        model.addAttribute("list",lists);
        model.addAttribute("brandId",brandId);
        return "list";
    }
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        return "add";
    }
    @RequestMapping("/add")
    public String add(VehicleComplaints veh, HttpServletResponse response) throws IOException {
        int insert = vehicleComplaintsService.insert(veh);
        PrintWriter out = response.getWriter();
        if (insert>0){
            out.print("<script>alert('成功');</script>");
            return "redirect:/list";
        }else{
            out.print("<script>alert('失败');</script>");
            return "redirect:/toAdd";
        }
    }

}
